package com.yanq.liancar.net;

import com.yanq.liancar.bean.BaseBBean;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.CarBean;
import com.yanq.liancar.bean.FindMsgBean;
import com.yanq.liancar.bean.MessageModel;
import com.yanq.liancar.bean.OBDBean;
import com.yanq.liancar.bean.OBDModel;
import com.yanq.liancar.net.util.RetrofitUtil;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class HttpUtil {

    private static HttpUtil httpUtil = new HttpUtil();
    private ApiService apiService;

    private HttpUtil() {
        apiService = RetrofitUtil.getInstance().getApiService();
    }

    public static HttpUtil getInstance() {
        return httpUtil;
    }

    private <T> void toSubscribe(Subscriber<T> subscriber, Observable<T> observable) {
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getMessage(Subscriber<MessageModel> subscriber, String token, String type, int page, int num) {
        toSubscribe(subscriber, apiService.getMessage(token, type, page, num));
    }

    public void getFind(Subscriber<FindMsgBean> subscriber, String token, String type, int page, int num) {
        toSubscribe(subscriber, apiService.getFind(token, type, page, num));
    }

    public void putPushToken(Subscriber<BaseBean> subscriber, String token, String pushToken) {
        toSubscribe(subscriber, apiService.putPushToken(token, pushToken));
    }

    public void getOBD(Subscriber<OBDBean> subscriber, String token) {
        toSubscribe(subscriber, apiService.getOBD(token));
    }

    public void getCar(Subscriber<CarBean> subscriber, String token) {
        toSubscribe(subscriber, apiService.getCar(token));
    }

    public void bindObd(Subscriber<BaseBean> subscriber, String token, String driNum, String obdNum) {
        toSubscribe(subscriber, apiService.bindObd(token, driNum, obdNum));
    }

    public void unBindObd(Subscriber<BaseBean> subscriber, String token, String id) {
        toSubscribe(subscriber, apiService.unBindObd(token, id));
    }

    public void checkVersion(Subscriber<BaseBean> subscriber) {
        toSubscribe(subscriber, apiService.checkVersion());
    }
}